#region License Info
//Component of Cronos Package, http://www.codeplex.com/cronos
//Copyright (C) 2009 Anthony Brockwell

//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either version 2
//of the License, or (at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#endregion


using System;
using System.Drawing;

namespace ExtraControlLibrary
{
    public abstract class PlotBoxExtra
    {
        /// <summary>
        /// Draws the component on its parent TSPlotBox.
        /// </summary>
        public abstract void Render(TSPlotBox pb, Graphics g);
    }

    public class ColoredCircle : PlotBoxExtra
    {
        private double x0, y0;
        private double radius;
        private Color rimColor;

        public ColoredCircle(double x, double y, double radius, Color rimColor)
        {
            x0 = x;
            y0 = y;
            this.radius = radius;
            this.rimColor = rimColor;
        }

        public override void Render(TSPlotBox pb, Graphics g)
        {
            int xpixel = pb.MaptoXPixel(x0);
            int ypixel = pb.MaptoYPixel(y0);
            int ypixtest = pb.MaptoYPixel(y0 + radius);
            int pixelradius = Math.Abs(ypixtest - ypixel);
            int pr2 = pixelradius/2;

            using (Pen p = new Pen(rimColor))
                g.DrawEllipse(p, xpixel - pr2, ypixel - pr2, pixelradius, pixelradius);
        }
    }

    public class PlotBoxRectangle : PlotBoxExtra
    {
        private double x0, y0;
        private int pixelWidth, pixelHeight;
        private Brush interior;

        public PlotBoxRectangle(double x, double y, int pixelWidth, int pixelHeight, Brush interior)
        {
            x0 = x;
            y0 = y;
            this.pixelWidth = pixelWidth;
            this.pixelHeight = pixelHeight;
            this.interior = interior;
        }

        public override void Render(TSPlotBox pb, Graphics g)
        {
            int xpixel = pb.MaptoXPixel(x0);
            int ypixel = pb.MaptoYPixel(y0);
            var rect = new Rectangle(xpixel - pixelWidth/2, ypixel - pixelHeight/2, pixelWidth+1, pixelHeight+1);
            g.FillRectangle(interior, rect);            
        }
    }

    public class PlotBoxText : PlotBoxExtra
    {
        private string text;
        private double x, y;
        private Font font;
        private Brush brush;

        public PlotBoxText(double x, double y, string text, Font font, Brush brush)
        {
            this.text = text;
            this.x = x;
            this.y = y;
            this.font = font;
            this.brush = brush;
        }

        public override void Render(TSPlotBox pb, Graphics g)
        {
            int xpixel = pb.MaptoXPixel(x);
            int ypixel = pb.MaptoYPixel(y);
            var sz = g.MeasureString(text, font).ToSize();
            g.DrawString(text, font, brush, xpixel - sz.Width/2, ypixel - sz.Height/2, StringFormat.GenericDefault);
        }
    }

    public class PlotBoxTimeBand : PlotBoxExtra
    {
        private double x0, x1;
        private Color backgroundcolor;

        public PlotBoxTimeBand(DateTime t0, DateTime t1, Color background)
        {
            x0 = t0.Ticks;
            x1 = t1.Ticks;
            backgroundcolor = background;
        }

        public PlotBoxTimeBand(DateTime t0, DateTime t1)
        {
            x0 = t0.Ticks;
            x1 = t1.Ticks;
            backgroundcolor = Color.Pink;
        }

        public PlotBoxTimeBand(double x00, double x01, Color background)
        {
            x0 = x00;
            x1 = x01;
            backgroundcolor = background;
        }

        public PlotBoxTimeBand(double x00, double x01)
        {
            x0 = x00;
            x1 = x01;
            backgroundcolor = Color.Pink;
        }

        public override void Render(TSPlotBox pb, Graphics g)
        {
            int leftpixel = pb.MaptoXPixel(x0),
                rightpixel = pb.MaptoXPixel(x1);
            Rectangle grect = pb.GraphRectangle;
            int toppixel = grect.Top, bottompixel = grect.Bottom;

            using (Pen p = new Pen(backgroundcolor))
            using (Brush b = new SolidBrush(backgroundcolor))
            {
                g.DrawRectangle(p, leftpixel, toppixel + 1, (rightpixel - leftpixel), (bottompixel - toppixel - 2));
                g.FillRectangle(b, leftpixel, toppixel + 1, (rightpixel - leftpixel), (bottompixel - toppixel - 2));
            }
        }
    }
}